package cn.zhf.shop.service.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.zhf.shop.entity.Message;

import cn.zhf.shop.entity.search.ClassUserSearch;
import cn.zhf.shop.utils.WeChatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;


import cn.zhf.shop.mapper.ClassUserMapper;
import cn.zhf.shop.entity.ClassUser;
import cn.zhf.shop.service.ClassUserService;

import java.util.LinkedList;
import java.util.List;

@Service

public class ClassUserServiceImpl implements ClassUserService {

    @Value("${wechat.appId}")
    private String appId;

    @Value("${wechat.secret}")
    private String secret;

    @Resource
    private ClassUserMapper classUserMapper;

    private Logger logger = LoggerFactory.getLogger(getClass());


    @Override
    public Message<?> register(ClassUser c) {
        logger.info("用户注册：{}", JSONUtil.toJsonStr(c));
        return classUserMapper.insert(c) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message<?> loginByMiniProgram(String jsCode) {
        logger.info("微信登录：jsCode：{}", jsCode);
        String s = WeChatUtils.WechatLoginUtil(jsCode, appId, secret);
        JSONObject jsonObject = JSONUtil.parseObj(s);
        logger.info("微信登录返回结果：{}", s);
        if ("0".equals(jsonObject.getStr("errcode"))) {
            ClassUser classUser = classUserMapper.selectByOpenId(jsonObject.getStr("openid"));
            if (classUser != null) {
                return Message.success(null).add(classUser);
            }
            return Message.fail("新用户").add(jsonObject.getStr("openid"));
        } else {
            return Message.fail("微信登陆错误");
        }
    }

    @Override
    public Message<?> loginByCaptcha(String userId) {
        return null;
    }

    @Override
    public Message<List<ClassUser>> getAll() {

        logger.info("获取全部用户");
        List<ClassUser> classUserList = classUserMapper.selectAll();
        for (ClassUser classUser : classUserList) {
            classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
        }
        return Message.success(null).add(classUserList);
    }

    @Override
    public Message<ClassUser> getUserInfoByUserId(String userId) {
        logger.info("查找用户信息 userId：{}", userId);
        ClassUser classUser = classUserMapper.selectByPrimaryKey(userId);
        if (classUser != null) {
            classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
        }

        return classUser == null ? Message.fail("用户未找到") : Message.success(null).add(classUser);

    }

    @Override
    public Message<List<ClassUser>> getUserByOrgId(int orgId) {
        List<ClassUser> refund = classUserMapper.selectByOrg(orgId);
        return refund == null ? Message.fail("无记录") : Message.success(null).add(refund);
    }

    @Override
    public Message<ClassUser> getUserInfoByOpenId(String openId) {
        logger.info("查找用户信息 openid：{}", openId);
        ClassUser classUser = classUserMapper.selectByOpenId(openId);
        return classUser == null ? Message.fail("用户未找到") : Message.success(null).add(classUser);
    }

    @Override
    public Message<?> updateUserInfoByUserId(ClassUser c) {
        logger.info("更新用户信息 id：{}", c.getId());
        return classUserMapper.updateByPrimaryKeySelective(c) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message deleteByPrimaryKey(String id) {
        return classUserMapper.deleteByPrimaryKey(id) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public int insert(ClassUser record) {
        return classUserMapper.insert(record);
    }

    @Override
    public int insertSelective(ClassUser record) {
        return classUserMapper.insertSelective(record);
    }

    @Override
    public ClassUser selectByPrimaryKey(String id) {
        return classUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(ClassUser record) {
        return classUserMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(ClassUser record) {
        return classUserMapper.updateByPrimaryKey(record);
    }

    @Override
    public Message zhanghaoLogin(String id, String user_pwd) {
        ClassUser user = classUserMapper.selectByIdAndUserPwd(id, user_pwd);
        if (user != null) {
            if (user.getUserStatus() == 0) {
                return Message.success(null).add(user);
            } else if (user.getUserStatus() == -1) {
                return Message.fail("帐号失效！");
            }

        }
        return Message.fail("帐号密码不正确");


    }

    @Override
    public Message updateUserStatusById(Integer updatedUserStatus, String id) {
        return classUserMapper.updateUserStatusById(updatedUserStatus, id) > 0 ? Message.success(null) : Message.fail(null);
    }


    @Override
    public Message mohusearch(String str) {
        if (str.equals("男")) {
            List<ClassUser> classUserList = classUserMapper.selectByStuSex(1);
            for (ClassUser classUser : classUserList) {
                classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
            }
            return Message.success(null).add(classUserList);
        } else if (str.equals("女")) {
            List<ClassUser> classUserList = classUserMapper.selectByStuSex(-1);
            for (ClassUser classUser : classUserList) {
                classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
            }
            return Message.success(null).add(classUserList);

        } else if (str.equals("正常")) {
            List<ClassUser> classUserList = classUserMapper.selectByUserStatus(0);
            for (ClassUser classUser : classUserList) {
                classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
            }
            return Message.success(null).add(classUserList);

        } else if (str.equals("失效")) {
            List<ClassUser> classUserList = classUserMapper.selectByUserStatus(-1);
            for (ClassUser classUser : classUserList) {
                classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
            }
            return Message.success(null).add(classUserList);

        }
        List<ClassUser> classUserList = classUserMapper.selectByIdAndStuIdCardAndStuUserNameAndStuSexAndStuPhoneAndStuAgeAndUserStatus(str);
        for (ClassUser classUser : classUserList) {
            classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
        }
        return Message.success(null).add(classUserList);

    }


    @Override
    public Message<List<ClassUser>> selectByStuPhoneAndStuIdCardAndStuUserName(String stuPhone, String stuIdCard, String stuUserName) {

        List<ClassUser> classUserList = classUserMapper.selectByStuPhoneAndStuIdCardAndStuUserName(stuPhone, stuIdCard, stuUserName);
        for (ClassUser classUser : classUserList) {
            classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
        }
        return Message.success(null).add(classUserList);

    }

    @Override
    public Message mohuConditionSearch(ClassUserSearch classUserSearch) {

        List<ClassUser> classUserList = classUserMapper.selectByStuUserNameAndIdAndStuIdCard(classUserSearch);


        List<ClassUser> classUserList1 = new LinkedList<>();

        if (classUserList.size() != 0) {
            if (classUserSearch.getMinAge() != null) {
                if (classUserSearch.getMaxAge() != null) {
                    for (ClassUser classUser : classUserList) {
                        int Age = IdcardUtil.getAgeByIdCard(classUser.getStuIdCard());
                        if (classUserSearch.getMinAge() <= Age && Age <= classUserSearch.getMaxAge()) {
                            classUser.setStuAge(Age);
                            classUserList1.add(classUser);
                        }
                    }
                    return Message.success(null).add(classUserList1);

                } else if (classUserSearch.getMaxAge() == null) {
                    for (ClassUser classUser : classUserList) {
                        int Age = IdcardUtil.getAgeByIdCard(classUser.getStuIdCard());
                        if (classUserSearch.getMinAge() <= Age) {
                            classUser.setStuAge(Age);
                            classUserList1.add(classUser);
                        }
                    }
                    return Message.success(null).add(classUserList1);
                }
            } else if (classUserSearch.getMinAge() == null && classUserSearch.getMaxAge() != null) {
                for (ClassUser classUser : classUserList) {
                    int Age = IdcardUtil.getAgeByIdCard(classUser.getStuIdCard());
                    if (classUserSearch.getMinAge() >= Age) {
                        classUser.setStuAge(Age);
                        classUserList1.add(classUser);
                    }
                }
                return Message.success(null).add(classUserList1);
            }

            for (ClassUser classUser : classUserList) {
                classUser.setStuAge(IdcardUtil.getAgeByIdCard(classUser.getStuIdCard()));
                classUserList1.add(classUser);
            }
            return Message.success(null).add(classUserList1);
        }
        return Message.success(null).add(classUserList);
    }
}




